package com.simple.mipc.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class ClassUtil {

    public static Field[] getFields(Object value) {
        return getFields(value.getClass());
    }

    public static Field[] getFields(Class<?> clazz) {
        List<Field> list = new ArrayList<>();
        while (Objects.nonNull(clazz)) {
            assert clazz != null;
            Field[] fields = clazz.getDeclaredFields();
            list.addAll(Arrays.asList(fields));
            clazz = clazz.getSuperclass();
        }
        return list.toArray(new Field[0]);
    }

    public static Method[] getMethods(Object value) {
        return getMethods(value.getClass());
    }

    public static Method[] getMethods(Class<?> clazz) {
        List<Method> list = new ArrayList<>();
        while (Objects.nonNull(clazz)) {
            assert clazz != null;
            Method[] methods = clazz.getDeclaredMethods();
            list.addAll(Arrays.asList(methods));
            clazz = clazz.getSuperclass();
        }
        return list.toArray(new Method[0]);
    }
}
